<?php
class controllerAppointment extends ControllerMain implements iController
{
	protected $updateInfo = array(); //array with data for update action
	protected $appointment; //object of appointment
	protected $error; //error variable
	
	protected function setMainTemplate()
	{
		$this->mainTemplate = $this->templateDir.'appointmentWindow.html';
	}
	
	protected function callHeader()
	{
		//Empty header
	}
	
	protected function callSidebar()
	{
		//Empty sidebar
	}
	
	public function index()
	{
		$this->viewForm();
	}
	
	/*
	*Update appointment action
	*/
	public function update()
	{
		$this->validateUpdateInfo(); //Call method for validate request data for update
		if(empty($this->error))
		{
			$this->appointment = new Appointment();
			if(isset($this->post['occurance']) && $this->post['occurance'] == 'all')
			{
				if($result = $this->appointment
								  ->updateAppointmentOccurance($this->updateInfo))
				{
					if(is_array($result))
					{
						$this->error = View::errorWindow($result['error']);
						View::assign('content',$this->error);
					}
					else
					{
						$this->keys['result'] = 'Appointment successfully update.';
						$this->template = View::create($this->templateDir.'updateResult.html',$this->keys);
						View::assign('content',$this->template);
					}
				}
				else
				{
					$this->keys['result'] = 'Appointment successfully update.';
					$this->template = View::create($this->templateDir.'updateResult.html',$this->keys);
					View::assign('content',$this->template);
				}	
			}
			else
			{
				if($result = $this->appointment->updateAppointment($this->updateInfo))
				{
				
					if(is_array($result))
					{
						$this->error = View::errorWindow($result['error']);
						View::assign('content',$this->error);
					}
					else
					{
						$this->keys['result'] = 'Appointment successfully update.';
						$this->template = View::create($this->templateDir.'updateResult.html',$this->keys);
						View::assign('content',$this->template);
					}
				}
			}
		}
		else
		{
			View::assign('content',$this->error);
		}	
	}
	
	/*
	*Delete appointment action
	*/
	public function delete()
	{
		if(!empty($this->post['id']) && !empty($this->post['deleteSubmit']))
		{
			$this->appointment = new Appointment();
			if ($this->appointment->get($this->post['id']))
			{
				if(isset($this->post['occurance']) && $this->post['occurance'] == 'all')
				{
					if($this->appointment->deleteOccuranceAppointment($this->post['id']))
					{
						$this->keys['result'] = 'Appointments successfully deleted!';
						$this->template = View::create($this->templateDir.'deleteResult.html',$this->keys);
						View::assign('content',$this->template);
					}
					else
					{
						$this->error = View::errorWindow('Error delete.');
						View::assign('content',$this->error);	
					}	
				}
				else
				{
					if($this->appointment->delete($this->post['id']))
					{
						$this->keys['result'] = 'Appointment successfully deleted!';
						$this->template = View::create($this->templateDir.'deleteResult.html',$this->keys);
						View::assign('content',$this->template);
					}
					else
					{
						$this->error = View::errorWindow('Error delete.');
						View::assign('content',$this->error);
					}
				}
			}
			else
			{
				$this->error = View::errorWindow('Appointment don\'t exists');
				View::assign('content',$this->error);
			}
		}
		else
		{
			$this->error = View::errorWindow('You don\'t enter all information for delete.');
			View::assign('content',$this->error);
		}
	}
	
	/*
	*Validate request data for add appointment action
	*
	*
	*return $this->updateInfo || $this->error
	*/
	private function validateUpdateInfo()
	{
		if(!empty($this->post['id']) &&
		   !empty($this->post['employee']) &&
		   !empty($this->post['description']))
		{	
			if(!empty($this->post['startHour']) && !empty($this->post['startMinute']))
			{	
				if(!empty($this->post['startPartDay']))
				{
					if($this->post['startHour'] >= 1 &&
					   $this->post['startHour'] <= 12 &&
					   $this->post['startMinute'] >= 0 &&
					   $this->post['startMinute'] <= 59)
					{
						if($this->post['startPartDay'] == 'pm')
						{
							if($this->post['startHour'] != '12')
							{
								$this->updateInfo['startHour'] = $this->post['startHour'] + 12;
							}
							else
							{
								$this->updateInfo['startHour'] = 12;
							}	
						}
						elseif($this->post['startPartDay'] == 'am')
						{
							if($this->post['startHour'] == 12)
							{
								$this->updateInfo['startHour'] = 0;	
							}
							else
							{
								$this->updateInfo['startHour'] = $this->post['startHour'];
							}
						}
						else
						{
							$this->error .= View::errorWindow('You enter incorrect information about part of day in stat-time field.');
						}
						$this->updateInfo['startMinute'] = $this->post['startMinute'];
					}
					else
					{
						$this->error .= View::errorWindow('You enter incorrect information about start time of appointment.');	
					}	
				}
				else
				{
					if($this->post['startHour'] >= 0 &&
					   $this->post['startHour'] <= 23 &&
					   $this->post['startMinute'] >= 0 &&
					   $this->post['startMinute'] <= 59)
					{
						$this->updateInfo['startHour'] = $this->post['startHour'];
						$this->updateInfo['startMinute'] = $this->post['startMinute'];
					}
					else
					{
						$this->error .= View::errorWindow('You enter incorrect information about start time of appointment.');
					}	
				}
			}
			if(!empty($this->post['endHour']) && !empty($this->post['endMinute']))
			{
				if(!empty($this->post['endPartDay']))
				{
					if($this->post['endHour'] >= 1 &&
					   $this->post['endHour'] <= 12 &&
					   $this->post['endMinute'] >= 0 &&
					   $this->post['endMinute'] <= 59)
					{
						if($this->post['endPartDay'] == 'pm')
						{
							if($this->post['endHour'] != '12')
							{
								$this->updateInfo['endHour'] = $this->post['endHour'] + 12;
							}
							else
							{
								$this->updateInfo['endHour'] = 12;
							}	
						}
						elseif($this->post['endPartDay'] == 'am')
						{
							if($this->post['endHour'] == 12)
							{
								$this->updateInfo['endHour'] = 0;	
							}
							else
							{
								$this->updateInfo['endHour'] = $this->post['endHour'];
							}
						}
						else
						{
							$this->error .= View::errorWindow('You enter incorrect information about part of day in end time field.');
						}
						
						$this->updateInfo['endMinute'] = $this->post['endMinute'];
					}
					else
					{
						$this->error .= View::errorWindow('You enter incorrect information about end time of appointment.');	
					}	
				}
				else
				{
					if($this->post['endHour'] >= 0 &&
					   $this->post['endHour'] <= 23 &&
					   $this->post['endMinute'] >= 0 &&
					   $this->post['endMinute'] <= 59)
					{
						$this->updateInfo['endHour'] = $this->post['endHour'];
						$this->updateInfo['endMinute'] = $this->post['endMinute'];
					}
					else
					{
						$this->error .= View::errorWindow('You enter incorrect information about end time of appointment.');
					}	
				}
			}	
			$this->updateInfo['description'] = $this->post['description'];
			$this->updateInfo['employee'] = $this->post['employee'];
			$this->updateInfo['id'] = $this->post['id'];
		}
		else
		{
			$this->error .= View::errorWindow('You don\'t enter all information.');	
		}	
	}
	
	/*
	*View form for add new appointment
	*/
	protected function viewForm()
	{
		$this->appointment = new Appointment();
		if($this->appointment->get($this->get['id']))
		{
			$this->keys = $this->appointment->getAppointmentById($this->get['id']);
						$start = $this->keys['start'];
			$this->keys['nameemployee'] = $this->keys['name'];
			unset($this->keys['name']);
			$this->keys['idappointment'] = $this->get['id'];
			if(TIME_TYPE == 12)
			{
				$this->keys['start'] = date('g:i a',$this->keys['start']); 
				$this->keys['end'] = date('g:i a',$this->keys['end']);
				$this->keys['submitted'] = date('Y-m-d g:i a',$this->keys['submitted']);
				for($i=1;$i<=12;$i++)
				{
						$this->keys['hour'][$i]['numberHour'] = $i; 		
				}
			}
			elseif(TIME_TYPE == 24)
			{
				$this->keys['start'] = date('H:i',$this->keys['start']); 
				$this->keys['end'] = date('H:i',$this->keys['end']);
				$this->keys['submitted'] = date('Y-m-d H:i:s',$this->keys['submitted']);
				for($i=0;$i<=23;$i++)
				{
					if (0 == $i)
					{
						$this->keys['hour'][$i]['numberHour'] = '00'; 
					}
					else
					{
						$this->keys['hour'][$i]['numberHour'] = $i; 	
					}
				}
			}
			for($i=0;$i<60;$i++)
			{
				if($i == 0)
				{
					$this->keys['minute'][$i]['numberMinute'] = '00';
				}
				else
				{
					$this->keys['minute'][$i]['numberMinute'] = $i;
				}	
			}
			$this->employee = new Employee();
			$employees = $this->employee->getAllFields('id,name');
			$i = 0;
			foreach($employees as $key=>$value)
			{
				$i++;
				if($value['id'] != $this->keys['employee'])
				{
					$this->keys['employees'][$i]['employees'] = $value['id'];
					$this->keys['employees'][$i]['name'] = $value['name'];
				}	
			}
			if(!isset($this->keys['employees']) or empty($this->keys['employees']))
			{
				$this->keys['employees'][0]['employees'] = '';
				$this->keys['employees'][0]['name'] = '';
			}
			
			if($start > time())
			{	
				$this->template = View::create($this->templateDir.'viewForm.html',$this->keys);
				View::assign('content',$this->template);
			}
			else
			{
				$this->template = View::create($this->templateDir.'viewInfo.html',$this->keys);
				View::assign('content',$this->template);
			}	
		}
		else
		{
			$this->error = View::errorWindow('Appointment don\'t exists.');
			View::assign('content',$this->error);
		}
	}
}